﻿var zombies = zombies || {};
var windowURL = window.URL || window.webkitURL;

zombies.ExchangeHistory = function () {
    var self = this;
    self.Pages = ko.observableArray(); 
    self.Loaded = ko.observable(false);
    self.MemExchanges = ko.observableArray();
    self.CurrentPage = ko.observable();
    self.Avatar = ko.observable("../Advertise/DefaultAssets/NoAvatar.gif");
    self.FullName = ko.observable();

    self.ChangePage = function (page) {
        self.MemExchanges.removeAll();

        $.ajax({
            url: '/api/MemberProfile/GetExchangeLog/' + page,
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        }).done(function (request) {
            self.Pages.removeAll();
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                self.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                self.MemExchanges.push(new zombies.MemExchange(item));
            });
        }).fail(function (request) {
            self.Loaded(false);
        });
    }
};

zombies.MemExchange = function (item) {
    var self = this;

    self.No = item.No;
    self.DateExchange = moment(item.DateExchange).format('LLL');
    self.Name = item.Name;
    self.Quantity = item.Quantity;
    self.Pid = item.Pid;
    self.Status = item.Status;
    self.SttCss = item.Status ? 'alert-box success' : 'alert-box warning';
    self.SttText = item.Status ? 'Hoàn thành' : 'Chưa hoàn thành';
};

var load = function (cur) {

    $.ajax({
        type: "GET",
        url: "/api/MemberProfile/GetUserProfile",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (item) {
        if (item.Avatar != null && item.Avatar != '') {
            memExchangeViewModel.Avatar(item.Avatar);
        }
        memExchangeViewModel.FullName(item.FirstName + ' ' + item.LastName);
        memExchangeViewModel.Loaded(true);
    });

    if (memExchangeViewModel.Loaded) {
        memExchangeViewModel.CurrentPage(cur);
        memExchangeViewModel.MemExchanges.removeAll();

        $.ajax({
            url: '/api/MemberProfile/GetExchangeLog/' + cur,
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        }).done(function (request) {
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                memExchangeViewModel.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                memExchangeViewModel.MemExchanges.push(new zombies.MemExchange(item));
            });
        }).fail(function (request) {
            memExchangeViewModel.Loaded(false);
        });
    }
};

var memExchangeViewModel;
$(document).ready(function () {
    memExchangeViewModel = new zombies.ExchangeHistory();
    load(1);
    ko.applyBindings(memExchangeViewModel, document.getElementById("my-container"));
});